﻿using Compass.PurchaseService.Domain;
using Compass.PurchaseService.Domain.Entities;
using Compass.Wasm.Shared.Categories;
using Compass.Wasm.Shared.Wms;
using Microsoft.EntityFrameworkCore;

namespace Compass.PurchaseService.Infrastructure;

public class PurchaseRepository:IPurchaseRepository
{
    private readonly PurchaseDbContext _context;

    public PurchaseRepository(PurchaseDbContext context)
    {
        _context = context;
    }

    #region NavItem
    public Task<IQueryable<NavItem>> GetNavItemsAsync()
    {
        return Task.FromResult(_context.NavItems.AsQueryable());
    }

    public Task<NavItem?> GetNavItemByIdAsync(Guid id)
    {
        return _context.NavItems.SingleOrDefaultAsync(x => x.Id.Equals(id));
    }

    public Task<NavItem?> GetNavItemByNumberAsync(string number)
    {
        return _context.NavItems.SingleOrDefaultAsync(x => x.Number.Equals(number));
    }

    public Task<IQueryable<NavItem>> GetNavItemsByFsAndShareAsync()
    {
        return Task.FromResult(_context.NavItems.Where(x => x.Sbu == Sbu_e.FS || x.Sbu== Sbu_e.SHARE));
    }

    #endregion

    #region NavInvValuation
    public Task<IQueryable<NavInvValuation>> GetNavInvValuationsAsync()
    {
        return Task.FromResult(_context.NavInvValuations.AsQueryable());
    }

    public Task<NavInvValuation?> GetNavInvValuationByIdAsync(Guid id)
    {
        return _context.NavInvValuations.SingleOrDefaultAsync(x => x.Id.Equals(id));
    }

    public Task<NavInvValuation?> GetNavInvValuationByNumberAndMonthAsync(string number, DateTime month)
    {
        //相同年月即可
        return _context.NavInvValuations.SingleOrDefaultAsync(x =>
            x.Number.Equals(number)&& 
            x.YearMonth.Year==month.Year&&
            x.YearMonth.Month==month.Month);
    }

    public Task<IQueryable<NavInvValuation>> GetNavInvValuationsByNumberAndPeriodAsync(string number,DateTime start, DateTime end)
    {
        //当月第一天0时0分0秒：start.AddDays(1 - start.Day)
        //当月最后一天23时59分59秒：end.AddDays(1 - end.Day).AddMonths(1).AddSeconds(-1)
        var s = start.AddDays(1 - start.Day);
        var e = end.AddDays(1 - end.Day).AddMonths(1).AddSeconds(-1);

        return Task.FromResult(_context.NavInvValuations.Where(x =>
            x.Number.Equals(number)&&
            x.YearMonth >= s &&
            x.YearMonth <= e));
    }
    #endregion

    #region NavInvReportClass
    public Task<IQueryable<NavInvReportClass>> GetNavInvReportClassesAsync()
    {
        return Task.FromResult(_context.NavInvReportClasses.OrderBy(x=>x.YearMonth).AsQueryable());
    }

    public Task<NavInvReportClass?> GetNavInvReportClassByIdAsync(Guid id)
    {
        return _context.NavInvReportClasses.SingleOrDefaultAsync(x => x.Id.Equals(id));
    }

    public Task<NavInvReportClass?> GetNavInvReportClassByMonthAsync(DateTime month)
    {
        //相同年月即可
        return _context.NavInvReportClasses.SingleOrDefaultAsync(x =>
            x.YearMonth.Year==month.Year&&
            x.YearMonth.Month==month.Month);
    }



    #endregion

    #region NavInvReportGroup

    public Task<IQueryable<NavInvReportGroup>> GetNavInvReportGroupsAsync()
    {
        return Task.FromResult(_context.NavInvReportGroups.OrderBy(x => x.YearMonth).AsQueryable());
    }

    public Task<NavInvReportGroup?> GetNavInvReportGroupByIdAsync(Guid id)
    {
        return _context.NavInvReportGroups.SingleOrDefaultAsync(x => x.Id.Equals(id));
    }

    public Task<NavInvReportGroup?> GetNavInvReportGroupByMonthAndGroupAsync(DateTime month, Group_e group)
    {
        //相同年月即可
        return _context.NavInvReportGroups.SingleOrDefaultAsync(x =>
            x.Group==group &&
            x.YearMonth.Year==month.Year&&
            x.YearMonth.Month==month.Month);
    }

    #endregion
}